package com.atguigu.gmall.payment.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.payment.config.AlipayConfig;
import com.atguigu.gmall.payment.service.AlipayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * title:
 * author: Gyh
 * date:2023-05-10 18:14
 * Description:
 */
@RestController
@RequestMapping("api/payment/alipay")
public class AlipayController {

    @Autowired
    private AlipayService alipayService;

    /**
     * 调用支付宝接口产生支付页面
     * @param orderId
     * @return
     */
    @GetMapping("/submit/{orderId}")
    public String createAlipayForm(@PathVariable("orderId") Long orderId){
        String payFrom = alipayService.createAlipayForm(orderId);
        return payFrom;
    }

    /**
     * 处理支付成功后,同步回调,展示支付成功页面
     */
    @GetMapping("/callback/return")
    public void redirectPaySuccess(HttpServletResponse response) throws IOException {
        //重定向到支付成功页面
        response.sendRedirect(AlipayConfig.return_order_url);
    }

    /**
     * 处理用户支付成功后,支付宝异步通知用户支付结果接口
     * @param paramsMap 支付提交参数
     * @return "success" 支付宝收到该结果,停止通知,如果未响应"success"做到最大努力通知,额外通知7次
     */
    @PostMapping("/callback/notify")
    public String notifyGmallPayResult(@RequestParam Map<String,String> paramsMap){
        String result = alipayService.notifyGmallPayResult(paramsMap);
        return result;
    }

    /**
     * 双方协商一致后退款
     * @param orderId
     * @return
     */
    @GetMapping("/refund/{orderId}")
    public Result refund(@PathVariable("orderId") Long orderId){
        alipayService.refund(orderId);
        return Result.ok();
    }
}
